package com.suxin.predicate.predicate;

import java.util.Optional;
import java.util.function.Function;

/**
 * @author Tang
 * @classname FunctionPredicate
 * @description [  ]
 * @date 2022/11/2 10:19
 */
public class FunctionPredicate<T, M> implements Function<T, Optional<M>> {

    private Function<T, M> trFunction;

    private final DefaultPredicate<T> defaultPredicate;

    public FunctionPredicate(DefaultPredicate<T> defaultPredicate) {
        this.defaultPredicate = defaultPredicate;
    }

    public FunctionPredicate<T,M> addFunction(Function<T, M> trFunction) {
        this.trFunction = trFunction;
        return this;
    }

    @Override
    public Optional<M> apply(T t) {
        if (defaultPredicate.test(t)) {
            return Optional.ofNullable(this.trFunction.apply(t));
        }
        return Optional.empty();
    }

}
